package com.zuihai.app.mvp.presenter;

import android.app.Application;
import android.content.Context;
import android.util.TypedValue;
import android.view.WindowManager;

import com.jess.arms.di.scope.FragmentScope;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.widget.imageloader.ImageLoader;
import com.zuihai.app.mvp.contract.MyHomepageRecyclerContract;
import com.zuihai.app.mvp.model.entity.BaseJson;
import com.zuihai.app.mvp.model.entity.BaseJson.Result;
import com.zuihai.app.mvp.model.entity.HomeItem;
import com.zuihai.app.mvp.ui.adapter.HomeRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import rx.Observable;
import rx.functions.Action0;
import rx.functions.Func1;


/**
 * 通过Template生成对应页面的MVP和Dagger代码,请注意输入框中输入的名字必须相同
 * 由于每个项目包结构都不一定相同,所以每生成一个文件需要自己导入import包名,可以在设置中设置自动导入包名
 * 请在对应包下按以下顺序生成对应代码,Contract->Model->Presenter->Activity->Module->Component
 * 因为生成Activity时,Module和Component还没生成,但是Activity中有它们的引用,所以会报错,但是不用理会
 * 继续将Module和Component生成完后,编译一下项目再回到Activity,按提示修改一个方法名即可
 * 如果想生成Fragment的相关文件,则将上面构建顺序中的Activity换为Fragment,并将Component中inject方法的参数改为此Fragment
 */


/**
 * Created by Administrator on 2017/5/11 0011.
 */

@FragmentScope
public class MyHomepageRecyclerPresenter extends BasePresenter<MyHomepageRecyclerContract.Model, MyHomepageRecyclerContract.View> implements MyHomepageRecyclerContract.Presenter{
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;
    private List<HomeItem> mDatas = new ArrayList<>();
    private final HomeRecyclerAdapter mAdapter;

    @Inject
    public MyHomepageRecyclerPresenter(MyHomepageRecyclerContract.Model model, MyHomepageRecyclerContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mAppManager = appManager;
        WindowManager wm = (WindowManager) application
                .getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16+16, mApplication.getResources().getDisplayMetrics());
        mAdapter = new HomeRecyclerAdapter(mDatas,width-padding);
        mRootView.bindAdapter(mAdapter);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    @Override
    public void request() {
        mModel.request()
                .map(new Func1<BaseJson<HomeItem>, Result>() {
                    @Override
                    public Result call(BaseJson<HomeItem> baseJson) {
                        if (baseJson.isSuccess()) {
                            return baseJson.data;
                        }
                        return null;
                    }
                })
                .flatMap(new Func1<Result, Observable<HomeItem>>() {
                    @Override
                    public Observable<HomeItem> call(Result homeResult) {
                        return Observable.from(homeResult.result);
                    }
                })
                .doOnCompleted(new Action0() {
                    @Override
                    public void call() {
                        mAdapter.notifyDataSetChanged();
                    }
                })
                .subscribe(new ErrorHandleSubscriber<HomeItem>(mErrorHandler) {
                    @Override
                    public void onNext(HomeItem homeItem) {
                        mDatas.add(homeItem);
                    }
                });
    }
}